package com.da.workbench;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.da.finance.DaFinanceCategory;
import com.da.finance.DaFinanceStatement;
import com.da.landlord.DaAgreement;
import com.da.landlord.DaChargeBill;
import com.da.landlord.DaChargeItem;
import com.da.landlord.DaRoom;
import com.da.landlord.DaRoomService;
import com.espirit.eap.util.JsonService;
import com.googlecode.cswish.annotation.Rest;
import com.googlecode.cswish.oosearch.Condition;
import com.googlecode.cswish.oosearch.QLInfo;
import com.googlecode.cswish.oosearch.QLParse;
import com.googlecode.cswish.struts.spring.GenericService;
import com.ra.landlord.Agreement;
import com.ra.landlord.Fee;
import com.ra.landlord.LandlordAssistUser;
import com.ra.landlord.LandlordOwnerUser;
import com.ra.landlord.LandlordUser;
import com.ra.landlord.LandlordUserService;
import com.ra.landlord.Room;
import com.ra.system.Version;
import com.ra.util.DateTimeUtil;

@Service
public class StatisticsService {

	@Resource
	private DaRoomService roomService;
	@Resource
	private LandlordUserService landlordUserService;
	
	@Resource
	private GenericService genericService;
	
	@Resource
	private QLParse qlParse;
	
	@Resource
	private JsonService jsonService;
	
	/***
	 * 获取已出租房子的数量
	 * @return
	 */
	@Rest(simpleServiceName = "loginUser", type = "free")
	public Long getHasRentCount(){
		LandlordUser landlordUser = landlordUserService.getLandlordUser();
		LandlordOwnerUser landlordOwnerUser = landlordUserService.getLandlordOwnerUser();
		if (landlordUser instanceof LandlordAssistUser) {
			QLInfo ql1 = new QLInfo("select count(a.id) from " + DaRoom.class.getName() + " a where a.rentState = " + DaRoom.RENT_STATE_RENT + "and a.landlordUser = ? and exists (from a.suite.landlordUsers b where b = ?)", true, landlordOwnerUser,landlordUser);
			QLInfo ql2 = new QLInfo("select count(a.id) from " + Room.class.getName() +" a where a.rentState = ? and a.status = ? " + "and a.landlordUser = ? and exists (from a.building.landlordUsers b where b = ?)", true);
			ql2.addParam(Room.RENT_STATE_ALREADY);
			ql2.addParam(Version.STATUS_VALID);
			ql2.addParam(landlordOwnerUser);
			ql2.addParam(landlordUser);
			return (Long) genericService.searchByQl(ql1, true).getData().get(0) + (Long) genericService.searchByQl(ql2, true).getData().get(0);
		} else {
			QLInfo ql1 = new QLInfo("select count(a.id) from " + DaRoom.class.getName() + " a where a.rentState = " + DaRoom.RENT_STATE_RENT, false);
			QLInfo ql2 = new QLInfo("select count(a.id) from " + Room.class.getName() +" a where a.rentState = ? and a.status = ? ", false);
			ql2.addParam(Room.RENT_STATE_ALREADY);
			ql2.addParam(Version.STATUS_VALID);
			return (Long) genericService.searchByQl(ql1, true).getData().get(0) + (Long) genericService.searchByQl(ql2, true).getData().get(0);
		}
		
	}
	
	/**
	 * 获取未出租房子的数量
	 * @return
	 */
	@Rest(simpleServiceName = "loginUser", type = "free")
	public Long getNeedRentCount(){
		LandlordUser landlordUser = landlordUserService.getLandlordUser();
		LandlordOwnerUser landlordOwnerUser = landlordUserService.getLandlordOwnerUser();
		if (landlordUser instanceof LandlordAssistUser) {
			QLInfo ql1 = new QLInfo("select count(a.id) from " + DaRoom.class.getName() + " a where a.rentState = " + DaRoom.RENT_STATE_UNRENT + "and a.landlordUser = ? and exists (from a.suite.landlordUsers b where b = ?)", true, landlordOwnerUser,landlordUser);
			QLInfo ql2 = new QLInfo("select count(a.id) from " + Room.class.getName() +" a where a.rentState = ? and a.status = ? " + "and a.landlordUser = ? and exists (from a.building.landlordUsers b where b = ?)", true);
			ql2.addParam(Room.RENT_STATE_NOYET);
			ql2.addParam(Version.STATUS_VALID);
			ql2.addParam(landlordOwnerUser);
			ql2.addParam(landlordUser);
			return (Long) genericService.searchByQl(ql1, true).getData().get(0) + (Long) genericService.searchByQl(ql2, true).getData().get(0);
		} else {
			QLInfo ql1 = new QLInfo("select count(a.id) from " + DaRoom.class.getName() + " a where a.rentState = " + DaRoom.RENT_STATE_UNRENT, false);
			QLInfo ql2 = new QLInfo("select count(a.id) from " + Room.class.getName() +" a where a.rentState = ? and a.status = ? ", false);
			ql2.addParam(Room.RENT_STATE_NOYET);
			ql2.addParam(Version.STATUS_VALID);
			return (Long) genericService.searchByQl(ql1, true).getData().get(0) + (Long) genericService.searchByQl(ql2, true).getData().get(0);
		}
	}
	
	/**
	 * 获取已收租的房子数
	 * @return
	 */
	@Rest(simpleServiceName = "loginUser", type = "free")
	public Long getHasPaiedCount(){
		LandlordUser landlordUser = landlordUserService.getLandlordUser();
		LandlordOwnerUser landlordOwnerUser = landlordUserService.getLandlordOwnerUser();
		if (landlordUser instanceof LandlordAssistUser) {
			QLInfo ql1 = new QLInfo("from " + DaRoom.class.getName() + " a inner join a.agreement b where a.landlordUser = ? and exists (from a.suite.landlordUsers f where f = ?) and exists (from " + DaChargeBill.class.getName() + " c inner join c.chargeItems d with d.chargeType = ? where a.agreement = c.agreement and date_add_interval(d.dateEndRecv, -b.advanceNum) >= ? )  and b.agreementState=0", true);
			ql1.addParam(landlordOwnerUser);
			ql1.addParam(landlordUser);
			ql1.addParam(DaChargeItem.CHARGE_TYPE_RENTAL);
			ql1.addParam(DateTimeUtil.throwTimeForDate(new Date()));
			QLInfo ql2 = new QLInfo("from " + Room.class.getName() + " a inner join a.agreements b with a.rentState=? where a.landlordUser = ? and exists (from a.building.landlordUsers f where f = ?) and exists (from b.fees c where substr(date_add_interval(c.dateEndRecv, -ifnull(b.advanceNum, 3)), 1, 10) >= substr(?, 1, 10) and c.feeType = ?) and b.agreementState=? ", true);
			ql2.addParam(Room.RENT_STATE_ALREADY);
			ql2.addParam(landlordOwnerUser);
			ql2.addParam(landlordUser);
			ql2.addParam(DateTimeUtil.formatDate(new Date()));
			ql2.addParam(Fee.FEE_TYPE_RENTAL);
			ql2.addParam(Agreement.AGREEMENT_STATE_VALID);
			int count1 = genericService.getCount(ql1, false);
			int count2 = genericService.getCount(ql2, false);
			return new Long(count1 + count2);
		} else {
			QLInfo ql1 = new QLInfo("from " + DaRoom.class.getName() + " a inner join a.agreement b where exists (from " + DaChargeBill.class.getName() + " c inner join c.chargeItems d with d.chargeType = ? where a.agreement = c.agreement and date_add_interval(d.dateEndRecv, -b.advanceNum) >= ? )  and b.agreementState=0", false);
			ql1.addParam(DaChargeItem.CHARGE_TYPE_RENTAL);
			ql1.addParam(DateTimeUtil.throwTimeForDate(new Date()));
			QLInfo ql2 = new QLInfo("from " + Room.class.getName() + " a inner join a.agreements b with a.rentState=? where exists (from b.fees c where substr(date_add_interval(c.dateEndRecv, -ifnull(b.advanceNum, 3)), 1, 10) >= substr(?, 1, 10) and c.feeType = ?) and b.agreementState=? ", false);
			ql2.addParam(Room.RENT_STATE_ALREADY);
			ql2.addParam(DateTimeUtil.formatDate(new Date()));
			ql2.addParam(Fee.FEE_TYPE_RENTAL);
			ql2.addParam(Agreement.AGREEMENT_STATE_VALID);
			int count1 = genericService.getCount(ql1, false);
			int count2 = genericService.getCount(ql2, false);
			return new Long(count1 + count2);	
		}
		
	}

	/**
	 * 获取未收租的房子数
	 * @return
	 */
	@Rest(simpleServiceName = "loginUser", type = "free")
	public Long getNeedPayCount(){
		LandlordUser landlordUser = landlordUserService.getLandlordUser();
		LandlordOwnerUser landlordOwnerUser = landlordUserService.getLandlordOwnerUser();
		if (landlordUser instanceof LandlordAssistUser) {
			QLInfo ql1 = new QLInfo("from " + DaRoom.class.getName() + " a inner join a.agreement b where a.landlordUser = ? and exists (from a.suite.landlordUsers f where f = ?) and  not exists (from " + DaChargeBill.class.getName() + " c inner join c.chargeItems d with d.chargeType = ? where a.agreement = c.agreement and date_add_interval(d.dateEndRecv, -b.advanceNum) >= ? )  and b.agreementState=0", false);
			ql1.addParam(landlordOwnerUser);
			ql1.addParam(landlordUser);
			ql1.addParam(DaChargeItem.CHARGE_TYPE_RENTAL);
			ql1.addParam(DateTimeUtil.throwTimeForDate(new Date()));
			QLInfo ql2 = new QLInfo("from " + Room.class.getName() + " a inner join a.agreements b with a.rentState=? where a.landlordUser = ? and exists (from a.building.landlordUsers f where f = ?) and not exists (from b.fees c where substr(date_add_interval(c.dateEndRecv, -ifnull(b.advanceNum, 3)), 1, 10) >= substr(?, 1, 10) and c.feeType = ? ) and b.agreementState=? ", false);
			ql2.addParam(Room.RENT_STATE_ALREADY);
			ql2.addParam(landlordOwnerUser);
			ql2.addParam(landlordUser);
			ql2.addParam(DateTimeUtil.formatDate(new Date()));
			ql2.addParam(Fee.FEE_TYPE_RENTAL);
			ql2.addParam(Agreement.AGREEMENT_STATE_VALID);
			int count1 = genericService.getCount(ql1, false);
			int count2 = genericService.getCount(ql2, false);
			return new Long(count1 + count2);
		} else {
			QLInfo ql1 = new QLInfo("from " + DaRoom.class.getName() + " a inner join a.agreement b where not exists (from " + DaChargeBill.class.getName() + " c inner join c.chargeItems d with d.chargeType = ? where a.agreement = c.agreement and date_add_interval(d.dateEndRecv, -b.advanceNum) >= ? )  and b.agreementState=0", false);
			ql1.addParam(DaChargeItem.CHARGE_TYPE_RENTAL);
			ql1.addParam(DateTimeUtil.throwTimeForDate(new Date()));
			QLInfo ql2 = new QLInfo("from " + Room.class.getName() + " a inner join a.agreements b with a.rentState=? where not exists (from b.fees c where substr(date_add_interval(c.dateEndRecv, -ifnull(b.advanceNum, 3)), 1, 10) >= substr(?, 1, 10) and c.feeType = ? ) and b.agreementState=? ", false);
			ql2.addParam(Room.RENT_STATE_ALREADY);
			ql2.addParam(DateTimeUtil.formatDate(new Date()));
			ql2.addParam(Fee.FEE_TYPE_RENTAL);
			ql2.addParam(Agreement.AGREEMENT_STATE_VALID);
			int count1 = genericService.getCount(ql1, false);
			int count2 = genericService.getCount(ql2, false);
			return new Long(count1 + count2);
		}
		
	}
	
	/**
	 * 合约已到期的数量
	 * @return
	 * @throws ParseException 
	 */
	@Rest(simpleServiceName = "loginUser", type = "free")
	public Long getAgreementExpiredCount() throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		LandlordUser landlordUser = landlordUserService.getLandlordUser();
		LandlordOwnerUser landlordOwnerUser = landlordUserService.getLandlordOwnerUser();
		if (landlordUser instanceof LandlordAssistUser) {
			QLInfo ql1 = new QLInfo("select count(a.id) from " + DaRoom.class.getName() + " a where a.landlordUser = ? and exists (from a.suite.landlordUsers f where f = ?) and a.rentState = ? and exists (select b.id from a.agreements b where b.agreementState = ? and b.rentEndDate <= ? )", false);
			ql1.addParam(landlordOwnerUser);
			ql1.addParam(landlordUser);
			ql1.addParam(DaRoom.RENT_STATE_RENT);
			ql1.addParam(DaAgreement.AGREEMENT_STATE_VALID);
			ql1.addParam(new Date());
			QLInfo ql2 = new QLInfo("select count(a.id) from " + Room.class.getName() + " a where a.landlordUser = ? and exists (from a.building.landlordUsers f where f = ?) and a.rentState = ? and exists (select b.id from a.agreements b where b.agreementState = ? and b.rentEndDate <= ? )", false);
			ql2.addParam(landlordOwnerUser);
			ql2.addParam(landlordUser);
			ql2.addParam(Room.RENT_STATE_ALREADY);
			ql2.addParam(Agreement.AGREEMENT_STATE_VALID);
			ql2.addParam(DateTimeUtil.formatDate(new Date()));
			return (Long) genericService.searchByQl(ql1, true).getData().get(0) + (Long) genericService.searchByQl(ql2, true).getData().get(0);
		} else {
			QLInfo ql1 = new QLInfo("select count(a.id) from " + DaRoom.class.getName() + " a where a.rentState = ? and exists (select b.id from a.agreements b where b.agreementState = ? and b.rentEndDate <= ? )", false);
			ql1.addParam(DaRoom.RENT_STATE_RENT);
			ql1.addParam(DaAgreement.AGREEMENT_STATE_VALID);
			ql1.addParam(new Date());
			QLInfo ql2 = new QLInfo("select count(a.id) from " + Room.class.getName() + " a where a.rentState = ? and exists (select b.id from a.agreements b where b.agreementState = ? and b.rentEndDate <= ? )", false);
			ql2.addParam(Room.RENT_STATE_ALREADY);
			ql2.addParam(Agreement.AGREEMENT_STATE_VALID);
			ql2.addParam(DateTimeUtil.formatDate(new Date()));
			return (Long) genericService.searchByQl(ql1, true).getData().get(0) + (Long) genericService.searchByQl(ql2, true).getData().get(0);
		}
	}
	
	/**
	 * 获取未到期的房子数量
	 * @return
	 * @throws ParseException 
	 */
	@Rest(simpleServiceName = "loginUser", type = "free")
	public Long getAgreementNotExpiredCount() throws ParseException{
		LandlordUser landlordUser = landlordUserService.getLandlordUser();
		LandlordOwnerUser landlordOwnerUser = landlordUserService.getLandlordOwnerUser();
		if (landlordUser instanceof LandlordAssistUser) {
			QLInfo ql1 = new QLInfo("select count(a.id) from " + DaRoom.class.getName() + " a where where a.landlordUser = ? and exists (from a.suite.landlordUsers f where f = ?) and a.rentState = ? and exists (select b.id from a.agreements b where b.agreementState = ? and b.rentEndDate > ? )", false);
			ql1.addParam(landlordOwnerUser);
			ql1.addParam(landlordUser);
			ql1.addParam(DaRoom.RENT_STATE_RENT);
			ql1.addParam(DaAgreement.AGREEMENT_STATE_VALID);
			ql1.addParam(new Date());
			QLInfo ql2 = new QLInfo("select count(a.id) from " + Room.class.getName() + " a where where a.landlordUser = ? and exists (from a.building.landlordUsers f where f = ?) and a.rentState = ? and exists (select b.id from a.agreements b where b.agreementState = ? and b.rentEndDate > ? )", false);
			ql2.addParam(landlordOwnerUser);
			ql2.addParam(landlordUser);
			ql2.addParam(Room.RENT_STATE_ALREADY);
			ql2.addParam(Agreement.AGREEMENT_STATE_VALID);
			ql2.addParam(DateTimeUtil.formatDate(new Date()));
			return (Long) genericService.searchByQl(ql1, true).getData().get(0) + (Long) genericService.searchByQl(ql2, true).getData().get(0);
		} else {
			QLInfo ql1 = new QLInfo("select count(a.id) from " + DaRoom.class.getName() + " a where a.rentState = ? and exists (select b.id from a.agreements b where b.agreementState = ? and b.rentEndDate > ? )", false);
			ql1.addParam(DaRoom.RENT_STATE_RENT);
			ql1.addParam(DaAgreement.AGREEMENT_STATE_VALID);
			ql1.addParam(new Date());
			QLInfo ql2 = new QLInfo("select count(a.id) from " + Room.class.getName() + " a where a.rentState = ? and exists (select b.id from a.agreements b where b.agreementState = ? and b.rentEndDate > ? )", false);
			ql2.addParam(Room.RENT_STATE_ALREADY);
			ql2.addParam(Agreement.AGREEMENT_STATE_VALID);
			ql2.addParam(DateTimeUtil.formatDate(new Date()));
			return (Long) genericService.searchByQl(ql1, true).getData().get(0) + (Long) genericService.searchByQl(ql2, true).getData().get(0);
		}
	}
	
	/**
	 * 获取房间的总数
	 * @return
	 */
	/*@Rest(simpleServiceName = "loginUser", type = "free")
	public Long getRoomCount(){
		QLInfo qlInfo = new QLInfo("select count(*) from DaRoom", false);
		return (Long)genericService.searchByQl(qlInfo).getData().get(0);
	}*/
	
	@Rest(simpleServiceName = "loginUser", type = "free")
	public Map<String,BigDecimal> getIncomeAndExpenses(){
		Map<String,BigDecimal> result=new HashMap<String, BigDecimal>();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar start=Calendar.getInstance();
		Calendar end=Calendar.getInstance();
		QLInfo qlInfo=new QLInfo();
		
		//当月收入支出
		start.set(Calendar.DAY_OF_MONTH, 1);
		end.set(Calendar.DAY_OF_MONTH, end.getActualMaximum(Calendar.DAY_OF_MONTH));
		String ql="select sum(case when (b.paymentCategory = 1 and a.tradeMoney < 0) or (b.paymentCategory = 0 and a.tradeMoney < 0) then -a.tradeMoney else a.tradeMoney end) from " + DaFinanceStatement.class.getName() + " a inner join a.financeCategory b where a.tradeDate >= '"+sdf.format(start.getTime())+"' and DATE_FORMAT(a.tradeDate, '%Y-%m-%d') <= '"+sdf.format(end.getTime())+"' and ((b.paymentCategory = 1 and a.tradeMoney >= 0) or (b.paymentCategory = 0 and a.tradeMoney <= 0)) and a.status = " + DaFinanceStatement.STATUS_VALID;
		qlInfo.setQl(ql);
		qlInfo.setDisablePermissionFilter(false);
		BigDecimal thisMonthIncome = null;
		if (genericService.searchByQl(qlInfo).getData().get(0) != null) {
			thisMonthIncome=new BigDecimal(genericService.searchByQl(qlInfo).getData().get(0).toString());			
		} else {
			thisMonthIncome = BigDecimal.valueOf(0);
		}
		ql="select sum(case when (b.paymentCategory = 1 and a.tradeMoney < 0) or (b.paymentCategory = 0 and a.tradeMoney < 0) then -a.tradeMoney else a.tradeMoney end) from " + DaFinanceStatement.class.getName() + " a inner join a.financeCategory b where a.tradeDate >= '"+sdf.format(start.getTime())+"' and DATE_FORMAT(a.tradeDate, '%Y-%m-%d') <= '"+sdf.format(end.getTime())+"' and ((b.paymentCategory = 1 and a.tradeMoney < 0) or (b.paymentCategory = 0 and a.tradeMoney > 0)) and a.status = " + DaFinanceStatement.STATUS_VALID;
		qlInfo.setQl(ql);
		qlInfo.setDisablePermissionFilter(false);
		BigDecimal thisMonthExpenses = null;
		if (genericService.searchByQl(qlInfo).getData().get(0) != null) {
			thisMonthExpenses=new BigDecimal(genericService.searchByQl(qlInfo).getData().get(0).toString());			
		} else {
			thisMonthExpenses = BigDecimal.valueOf(0);
		}
		
		//近3月收入支出
		start.set(Calendar.MONTH, start.get(Calendar.MONTH)-2);
		ql="select sum(case when (b.paymentCategory = 1 and a.tradeMoney < 0) or (b.paymentCategory = 0 and a.tradeMoney < 0) then -a.tradeMoney else a.tradeMoney end) from " + DaFinanceStatement.class.getName() + " a inner join a.financeCategory b where a.tradeDate >= '"+sdf.format(start.getTime())+"' and DATE_FORMAT(a.tradeDate, '%Y-%m-%d') <= '"+sdf.format(end.getTime())+"' and ((b.paymentCategory = 1 and a.tradeMoney >= 0) or (b.paymentCategory = 0 and a.tradeMoney <= 0)) and a.status = " + DaFinanceStatement.STATUS_VALID;
		qlInfo.setQl(ql);
		qlInfo.setDisablePermissionFilter(false);
		BigDecimal recentlyIncomeInThreeMonth = null;
		if (genericService.searchByQl(qlInfo).getData().get(0) != null) {
			recentlyIncomeInThreeMonth=new BigDecimal(genericService.searchByQl(qlInfo).getData().get(0).toString());			
		} else {
			recentlyIncomeInThreeMonth = BigDecimal.valueOf(0);
		}
		ql="select sum(case when (b.paymentCategory = 1 and a.tradeMoney < 0) or (b.paymentCategory = 0 and a.tradeMoney < 0) then -a.tradeMoney else a.tradeMoney end) from " + DaFinanceStatement.class.getName() + " a inner join a.financeCategory b where a.tradeDate >= '"+sdf.format(start.getTime())+"' and DATE_FORMAT(a.tradeDate, '%Y-%m-%d') <= '"+sdf.format(end.getTime())+"' and ((b.paymentCategory = 1 and a.tradeMoney < 0) or (b.paymentCategory = 0 and a.tradeMoney > 0)) and a.status = " + DaFinanceStatement.STATUS_VALID;
		qlInfo.setQl(ql);
		qlInfo.setDisablePermissionFilter(false);
		BigDecimal recentlyExpensesInThreeMonth = null;
		if (genericService.searchByQl(qlInfo).getData().get(0) != null) {
			recentlyExpensesInThreeMonth=new BigDecimal(genericService.searchByQl(qlInfo).getData().get(0).toString());			
		} else {
			recentlyExpensesInThreeMonth = BigDecimal.valueOf(0);
		}
		
		//近一年收入支出
		start.set(Calendar.MONTH, start.get(Calendar.MONTH)-11);
		ql="select sum(case when (b.paymentCategory = 1 and a.tradeMoney < 0) or (b.paymentCategory = 0 and a.tradeMoney < 0) then -a.tradeMoney else a.tradeMoney end) from " + DaFinanceStatement.class.getName() + " a inner join a.financeCategory b where a.tradeDate >= '"+sdf.format(start.getTime())+"' and DATE_FORMAT(a.tradeDate, '%Y-%m-%d') <= '"+sdf.format(end.getTime())+"' and ((b.paymentCategory = 1 and a.tradeMoney >= 0) or (b.paymentCategory = 0 and a.tradeMoney <= 0)) and a.status = " + DaFinanceStatement.STATUS_VALID;
		qlInfo.setQl(ql);
		qlInfo.setDisablePermissionFilter(false);
		BigDecimal recentlyIncomeInAYear = null;
		if (genericService.searchByQl(qlInfo).getData().get(0) != null) {
			recentlyIncomeInAYear=new BigDecimal(genericService.searchByQl(qlInfo).getData().get(0).toString());			
		} else {
			recentlyIncomeInAYear = BigDecimal.valueOf(0);
		}
		ql="select sum(case when (b.paymentCategory = 1 and a.tradeMoney < 0) or (b.paymentCategory = 0 and a.tradeMoney < 0) then -a.tradeMoney else a.tradeMoney end) from " + DaFinanceStatement.class.getName() + " a inner join a.financeCategory b where a.tradeDate >= '"+sdf.format(start.getTime())+"' and DATE_FORMAT(a.tradeDate, '%Y-%m-%d') <= '"+sdf.format(end.getTime())+"' and ((b.paymentCategory = 1 and a.tradeMoney < 0) or (b.paymentCategory = 0 and a.tradeMoney > 0)) and a.status = " + DaFinanceStatement.STATUS_VALID;
		qlInfo.setQl(ql);
		qlInfo.setDisablePermissionFilter(false);
		BigDecimal recentlyExpensesInAYear = null;
		if (genericService.searchByQl(qlInfo).getData().get(0) != null) {
			recentlyExpensesInAYear=new BigDecimal(genericService.searchByQl(qlInfo).getData().get(0).toString());			
		} else {
			recentlyExpensesInAYear = BigDecimal.valueOf(0);
		}
		
		result.put("thisMonthIncome", thisMonthIncome);
		result.put("thisMonthExpenses",thisMonthExpenses);
		result.put("recentlyIncomeInThreeMonth", recentlyIncomeInThreeMonth);
		result.put("recentlyExpensesInThreeMonth",recentlyExpensesInThreeMonth);
		result.put("recentlyIncomeInAYear", recentlyIncomeInAYear);
		result.put("recentlyExpensesInAYear",recentlyExpensesInAYear);
		return result;
	}
	
	@Rest(simpleServiceName = "loginUser", type = "free")
	public String getLoginName(){
		return landlordUserService.getLoginUser().getName();
	}
	
}
